package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.myannotation.RequiredLogin;
import cn.wolfcode.wolf2w.query.TravelQueryObject;
import cn.wolfcode.wolf2w.redis.service.IRedisService;
import cn.wolfcode.wolf2w.redis.test.RedisByKey;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.UMEditorUploader;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/travels")
public class TravelsController {

    @Autowired
    private ITravelService travelService;
    @Autowired
    private IRedisService redisService;
    @Autowired
    private ITravelCommentService travelCommentService;


    @GetMapping("/query")
    public Object query(TravelQueryObject qo,HttpServletRequest request){
        String token = request.getHeader("token");
        String join = RedisByKey.USER_TOKEN.join(token);
        String user = redisService.getRedis(join);
        UserInfo userInfo = JSON.parseObject(user, UserInfo.class);
        if(userInfo!=null){
            String id = userInfo.getId();
            qo.setUserId(id);
        }
        return JsonResult.success(travelService.query(qo));
    }


    @GetMapping("/detail")
    public Object detail(String id){
        Travel travel = travelService.selectOne(id);
        return JsonResult.success(travel);
    }

    //上传图片
    @RequestMapping("/contentImage")
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }

    @PostMapping("/saveOrUpdate")
    @RequiredLogin
    public Object saveOrUpdate(Travel travel,HttpServletRequest request){
        //对用户id进行维护
        String token = request.getHeader("token");
        String token1 = RedisByKey.USER_TOKEN.join(token);
        String userInfo = redisService.getRedis(token1);
        UserInfo userInfo1 = JSON.parseObject(userInfo, UserInfo.class);
        String id = userInfo1.getId();
        travel.setUserId(id);
        travelService.saveOrUpdate(travel);
        return JsonResult.success(travel.getId());
    }

    ////游记明细显示
    //@GetMapping("/detail")
    //public Object list(String id){
    //    List<Travel> travels = travelService.queryByDestId();
    //    return JsonResult.success(travels);
    //}

    ////编辑回显
    //@GetMapping("/input")
    //public Object input(String id){
    //    Travel travel = travelService.input(id);
    //    return JsonResult.success(travel);
    //}

    //游记评论的添加
    @RequiredLogin//只有用户登录了，才能进行游记的评论
    @PostMapping("/commentAdd")
    public Object commentAdd(TravelComment travelComment,HttpServletRequest request){
        //获取当前登录用户对象
        String token = request.getHeader("token");
        String token1 = RedisByKey.USER_TOKEN.join(token);
        String userInfo = redisService.getRedis(token1);
        UserInfo userInfo1 = JSON.parseObject(userInfo, UserInfo.class);
        //维护游记内容的字段信息
        BeanUtils.copyProperties(userInfo1, travelComment);
        //维护用户id的字段信息
        travelComment.setUserId(userInfo1.getId());
        //设置游记评论时间
        travelComment.setCreateTime(new Date());
        //进行游记评论的保存业务
        travelCommentService.insert(travelComment);
        return JsonResult.success();
    }

    //游记评论列表的回显
    @GetMapping("/comments")
    public Object comments(String travelId){
        //调用业务方法查询评论内容，并返回
        List<TravelComment> travelComments=travelCommentService.queryComments(travelId);
        return JsonResult.success(travelComments);
    }

    //处理收藏功能、
    @RequiredLogin
    @PostMapping("/favor")
    public Object favor(String travelId,HttpServletRequest request){
        String token = request.getHeader("token");
        String token1 = RedisByKey.USER_TOKEN.join(token);
        String userInfo = redisService.getRedis(token1);
        UserInfo userInfo1 = JSON.parseObject(userInfo, UserInfo.class);
        String id = userInfo1.getId();
        boolean b=travelService.travelId(travelId,id);
        return JsonResult.success(b);
    }

}
